<h5><?php echo t('page', 'Authy 2-Factor Authentication Configuration'); ?></h5>

<div class="row-fluid">
	<div class="span12 hide" id="authy_info">
		<div class="alert alter-block alert-info">
<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
			<div class="content"></div>
		</div>
	</div>
</div>
<div id="authy-block">
	<form id="individual-authy-form">
		<div class="row-fluid">
			<?php echo CHtml::tag('span', array('class' => 'input uneditable-input'), $user->email) ?>
			<span class="help-inline">Your user email addres.</span>
		</div>
		
		<div class="row-fluid">
			<input id="authy-countries" name="countryCode" placeholder="Country">
			<span class="help-inline">Country code for your cell phone.</span>
		</div>
		
		<div class="row-fluid">
			<input type="text" id="authy-cellphone" name="Cellphone" value="" placeholder="Cellphone" />
			<span class="help-inline">Cell phone number.</span>
		</div>
		
		<div class="row-fluid">
			<button class="btn btn-small btn-primary" id="authy_btn" type="button"><?php echo t('buttons', 'Enable Authy Authentication'); ?></button>
		</div>
	</form>
	
	<script>
		var authyRequest = function(){
			this.requestUrl = '';
			this.id_users = null;
			this.countryCode = null;
			this.Cellphone = null;
	
			this.beforeRequest = function(id_user, doTimes){};
			this.requestCallback = function(id_user, serverReturn, doTimes){};
			this.afterDone = function(){};
	
			this.doTimes = 0;
			this.do = function(){
				this.doTimes++;
				var id_user = this.id_users.pop();
				if(!id_user){
					this.afterDone();
					return false;
				}
				this.beforeRequest(id_user, this.doTimes);
				var thisObj = this;
				$.ajax({
					type: 'post',
					url: thisObj.requestUrl,
					data: {
						id_user: id_user,
						countryCode: thisObj.countryCode,
						Cellphone: thisObj.Cellphone
					},
					success: function(ret){
						thisObj.requestCallback(id_user, ret, thisObj.doTimes);
						thisObj.do();
						return true;
					},
					dataType: 'json'
				});
			};
		};
	
		$('#individual-authy-form #authy_btn').click(function(){
			var countryCode = $('#individual-authy-form [name="countryCode"]').val();
			var Cellphone = $('#individual-authy-form #authy-cellphone').val();
			var id_users = [];
			if($('#id_users').val() == ''){
				$('#id_users option[value!=""]').each(function(k, v){
					id_users.push($(v).val());
				});
			}else{
				id_users.push($('#id_users').val());
			}
	
			$('#authy_info').blink({
				'beforeCall': function(){
					$('#authy_info .content').append(
						'<p><b>Status ('+id_users.length+' Users)</b></p>'
					);
				},
				'callBack': function(){
					var authyRequester = new authyRequest();
					var length = id_users.length;
					authyRequester.countryCode = countryCode;
					authyRequester.Cellphone = Cellphone;
					authyRequester.id_users = id_users;
	
					authyRequester.requestUrl = <?php echo json_encode(url('user/authy')); ?>;
					authyRequester.beforeRequest = function(id_user, doTimes){
						$('#individual-authy-form #authy_btn').button('Processing...');
						$('#authy_info .content').append(
							'<p>'+$('#individual-authy-form #id_users option[value="'+id_user+'"]').text()+': Waiting...'+' ('+doTimes+' of '+length+' Users)</p>'
						);
					};
					authyRequester.afterDone = function(){
						$('#individual-authy-form #authy_btn').button('reset');
					}
					authyRequester.requestCallback = function(id_user, serverReturn, doTimes){
						if(serverReturn.result){
							var fullname = $('#individual-authy-form #id_users option[value="'+id_user+'"]').text();
							var result = '<p class="text-success accordion-toggle" data-toggle="collapse">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+fullname+' - Success</b></p>';
							$('#authy_info .content').append(result);

							$('#cancel-authy-block').show();
							$('#authy-block').hide();
						}else{
							$.each(serverReturn.errors, function(attrName, error){
								$(error).each(function(k, e){
									$('#authy_info .content').append(
										'<p class="text-error">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+$('#individual-authy-form #id_users option[value="'+id_user+'"]').text()+' - '+e+' ('+doTimes+' of '+length+' Users)</b></p>'
									);
								});
							});
						}
					};
					authyRequester.do();
				},
				'showTime': 0
			});
			
		});
	</script>
</div>


<div id="cancel-authy-block">
	<div class="btn-group">
		<button class="btn btn-info dropdown-toggle" id="user_authy">Disable 2-Factor Auth</button>
		<div class="hide" id="disableauthy_confirm_btn">
			<a class="btn btn-small" id="disableauthy_confirm"><i class="icon-ok"></i>Confirm Disable</a>
			<a class="btn btn-small" id="disableauthy_cancel"><i class="icon-remove"></i>Cancel</a>
		</div>
	</div>
	
	<script>
		$('#user_authy').click(function(){
			$(this).hide();
			$('#disableauthy_confirm_btn').show();
		
			return true;
		});
		
		$('#disableauthy_cancel').click(function(){
			$('#user_authy').show();
			$('#disableauthy_confirm_btn').hide();
		});
		
		$('#disableauthy_confirm').click(function(){
			var id_user = $('#id_users').val();
			$(this).parents('.btn-group').find('button').attr('data-loading-text', 'Disable authy...').button('loading');
			var obj = $(this);
			$.ajax({
				url: '<?php echo url('user/disableauthy'); ?>',
				type: 'post',
				data: {id_user: id_user},
				success: function(ret){
					obj.parents('.btn-group').find('button').button('reset');
					obj.parents('.btn-group').find('#disableauthy_cancel').click();
					if(ret.result){
						obj.parents('.btn-group').find('#user_authy a').attr('id', 'setup');
						obj.parents('.btn-group').find('#user_authy a').text('Setup 2-Factor Auth');

						$('#cancel-authy-block').hide();
						$('#authy-block').show();
					}else{
						$("#authy_info").blink({
							"beforeCall": function(){
								$("#authy_info .content").html("");
								$.each(ret["errors"], function(attrName, error){
									$(error).each(function(k, e){
										var p = $("<p />").addClass("text-error").append(attrName+": "+e);
										$("#authy_info .content").append(p);
									});
								});
							}
						});
					}
					obj.parents('.btn-group').find('.delete_cancel').click();
				},
				dataType: 'json'
			});
		});
	</script>
</div>

<script>
	<?php if(empty($user->authy_id)): ?>
		$('#cancel-authy-block').hide();
	<?php else: ?>
		$('#authy-block').hide();
	<?php endif; ?>
</script>